﻿using System.Linq;
using VO;
using System.Collections.Generic;

namespace DAL
{
    public class StatusAtendimentoDAL
    {

        #region Insert
        public void Insert(StatusAtendimento obj)
        {
            var contexto = new CallcenterEntities();
            var objC = new tbCCStatusAtendimento();
            objC = PopulaObjeto(obj, objC);
            contexto.AddTotbCCStatusAtendimento(objC);
            contexto.SaveChanges();
        }
        #endregion

        #region Update
        public void Update(StatusAtendimento obj)
        {
            var contexto = new CallcenterEntities();
            var status = (from statusAux in contexto.tbCCStatusAtendimento
                          where statusAux.idStatus == obj.idStatus
                          select statusAux).First();
            PopulaObjeto(obj, status);
            contexto.SaveChanges();
        }
        #endregion

        #region UpdateStatus
        public void UpdateStatus(StatusAtendimento obj)
        {
            var contexto = new CallcenterEntities();
            var campanha = (from statusAux in contexto.tbCCStatusAtendimento
                            where statusAux.idStatus == obj.idStatus
                            select statusAux).FirstOrDefault();
            campanha.fgAtivo = obj.fgAtivo;
            contexto.SaveChanges();
        }
        #endregion

        #region Delete
        public void Delete(int idStatus)
        {
            var contexto = new CallcenterEntities();
            var campanha = (from statusAux in contexto.tbCCStatusAtendimento
                            where statusAux.idStatus == idStatus
                            select statusAux).FirstOrDefault();
            contexto.DeleteObject(campanha);
            contexto.SaveChanges();
        }
        #endregion

        #region Select
        public IList<StatusAtendimento> Select(string nomeStatus = "")
        {
            var contexto = new CallcenterEntities();

            IQueryable<tbCCStatusAtendimento> status;

            if (string.IsNullOrWhiteSpace(nomeStatus))
            {
                status = (from statusAux in contexto.tbCCStatusAtendimento
                          where statusAux.fgAtivo
                          select statusAux);
            }
            else
            {
                status = (from statusAux in contexto.tbCCStatusAtendimento
                          where statusAux.nome == nomeStatus
                          select statusAux);
            }

            //Carrega lista
            var list = PopulaObjeto(status);

            return list;
        }

        public IList<StatusAtendimento> Select(int idStatus)
        {
            var contexto = new CallcenterEntities();

            IQueryable<tbCCStatusAtendimento> status;

            status = (from statusAux in contexto.tbCCStatusAtendimento
                      where statusAux.idStatus == idStatus
                      select statusAux);

            //Carrega lista
            var list = PopulaObjeto(status);

            return list;
        }
        #endregion

        #region PopulaObjeto
        protected static tbCCStatusAtendimento PopulaObjeto(StatusAtendimento obj, tbCCStatusAtendimento tabela)
        {
            tabela.nome = obj.nome;
            tabela.descricao = obj.descricao;
            tabela.fgAtivo = obj.fgAtivo;
            return tabela;
        }

        protected static IList<StatusAtendimento> PopulaObjeto(IQueryable<tbCCStatusAtendimento> reader)
        {
            IList<StatusAtendimento> list = new List<StatusAtendimento>();
            foreach (var obj in reader)
            {
                var newObj = new StatusAtendimento();
                newObj.idStatus = obj.idStatus;
                newObj.nome = obj.nome;
                newObj.descricao = obj.descricao;
                newObj.fgAtivo = obj.fgAtivo;
                list.Add(newObj);
            }
            return list;
        }
        #endregion

    }
}